package com.campus.controller;

import com.campus.dto.ClassConditionDto;
import com.campus.model.Class;
import com.campus.service.ClassService;
import com.campus.web.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/homework/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    // 创建教学班
    @PostMapping("/create")
    public ResponseResult<String> createClass(@RequestBody Class clazz) {
        int classId = classService.createClass(clazz);
        if (classId > 0) {
            return ResponseResult.ok("教学班创建成功，ID: " + classId);
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "教学班创建失败");
        }
    }

    // 更新教学班信息
    @PutMapping("/update/{id}")
    public ResponseResult<String> updateClass(@PathVariable int id, @RequestBody Class clazz) {
        clazz.setId(id);  // 确保传入的 ID 会被设置到要更新的对象中
        boolean success = classService.updateClass(clazz);
        if (success) {
            return ResponseResult.ok("教学班更新成功");
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "教学班更新失败");
        }
    }

    // 删除教学班
    @DeleteMapping("/delete/{classId}")
    public ResponseResult<String> deleteClass(@PathVariable int classId) {
        boolean result = classService.deleteClass(classId);
        if (result) {
            return ResponseResult.ok("教学班删除成功");
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "教学班删除失败");
        }
    }

    // 添加学生到教学班
    @PostMapping("/add-students")
    public ResponseResult<String> addStudentsToClass(
            @RequestParam int classId,
            @RequestParam List<Integer> studentIds) {
        String result = classService.addStudentsToClass(classId, studentIds);
        return ResponseResult.ok(result);
    }

    // 根据活动ID查询教学班
    @GetMapping("/query")
    public ResponseResult<List<Class>> getClassesByActivityId(@RequestParam int activityId) {
        List<Class> classes = classService.getClassesByActivityId(activityId);
        if (classes != null && !classes.isEmpty()) {
            return ResponseResult.ok(classes, "教学班查询成功");
        } else {
            return ResponseResult.error(HttpStatus.NOT_FOUND, "未找到相关教学班");
        }
    }

    // 根据条件查询教学班
    @PostMapping("/query-by-condition")
    public ResponseResult<List<Class>> getClassesByCondition(@RequestBody ClassConditionDto condition) {
        List<Class> classes = classService.getClassesByCondition(condition);
        if (classes != null && !classes.isEmpty()) {
            return ResponseResult.ok(classes, "教学班条件查询成功");
        } else {
            return ResponseResult.error(HttpStatus.NOT_FOUND, "未找到符合条件的教学班");
        }
    }

    // 查询教学班的所有学生
    @GetMapping("/students")
    public ResponseResult<List<Integer>> getStudentsByClassId(@RequestParam int classId) {
        List<Integer> students = classService.getStudentsByClassId(classId);
        return ResponseResult.ok(students, "学生列表查询成功");
    }

    // 查询所有教学班
    @GetMapping("/list")
    public ResponseResult<List<Class>> listAllClasses() {
        List<Class> classes = classService.getAllClasses();
        if (classes != null && !classes.isEmpty()) {
            return ResponseResult.ok(classes, "所有教学班查询成功");
        } else {
            return ResponseResult.error(HttpStatus.NOT_FOUND, "未找到任何教学班");
        }
    }
}
